package com.yanggu.mapreduce.serializable;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2019/12/14 22:02
 * @description:
 */
public class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    private Text k = new Text();

    private FlowBean v = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1. 获取整行的数据
        String line = value.toString();

        String[] fields = line.split("\t");

        int length = fields.length;

        //2. 设置key
        k.set(fields[1]);

        //3. 设置value
        v.setUpFlow(Long.parseLong(fields[length - 3]));
        v.setDownFlow(Long.parseLong(fields[length - 2]));

        //4. 写出
        context.write(k, v);
    }
}
